let ids = [];

let idsMaxLength = 20;
function addIds(id) {
  if (!ids.includes(id)) {
    ids.push(id)
  } else {
    return false
  }
}
function Collect() {
  let contentList = document.querySelectorAll('.feeds-container section');
  contentList.forEach(dom => {
    let a = dom.querySelector('div a');
    if (a) {
      // console.log(a,'a.href')
      let arr = a.href.split('/');
      let targetId = arr[arr.length - 1];
      // console.log(targetId,'targetId')
      addIds(targetId);
      // 调用函数发送数据
      // const dataToSend = { key: ids , type:'Start'}; // 你要发送的数据对象
      // sendDataToServer(dataToSend);
    } else {
      //   console.log('没有找到')
    };
  })
}
// let time = setTimeout()
let nextSrollTop = window.document.scrollingElement.scrollTop;
let currentSrollTop = window.document.scrollingElement.scrollTop;
function fn() {
  // setout
  window.setTimeout(() => {
    let endContainer = document.querySelector('.end-container');
    if (!endContainer) {
      window.document.scrollingElement.scrollTop += 100;
      Collect();
      fn()
    } else {
      window.document.scrollingElement.scrollTop += 2000;
      Collect()
      const dataToSend = { key, data: ids }; // 你要发送的数据对象
      console.log('收集完毕：', dataToSend);
      chrome.runtime.sendMessage(
        { greeting: "SearchPageReadyEnd", data: dataToSend },
        function (response) {
          // console.log("收到来自后台的回复：" + response);
          // key = response.key;
          // fn()
        }
      );
    }

  }, 300)
}
// fn()
let key;
chrome.runtime.sendMessage(
  { greeting: "SearchPageReady" },
  function (response) {
    console.log("收到来自后台的回复：" + response);
    key = response.key;
    fn()
  }
);
// chrome.runtime.onMessage.addListener(
//   function (request, sender, sendResponse) {
//     if (request.greeting === '"StartCollectingNoteIds"') {
//       console.log('开始搜集笔记id')

//     }
//   });
const PORT = 8090;
const url = `http://localhost:${PORT}/data`;

// function sendDataToServer(data) {
//   // 设置fetch选项
//   const requestOptions = {
//     method: 'POST',
//     headers: {
//       'Content-Type': 'application/json'
//     },
//     body: JSON.stringify(data)
//   };

//   // 发起POST请求
//   fetch(url, requestOptions)
//     .then(response => {
//       // 检查响应状态
//       if (!response.ok) {
//         throw new Error('Network response was not ok');
//       }
//       // 解析JSON格式的响应数据
//       return response.json();
//     })
//     .then(responseData => {
//       // 处理接收到的响应数据
//       console.log('Received response from server:', responseData);
//     })
//     .catch(error => {
//       // 处理错误
//       console.error('There was a problem with the fetch operation:', error);
//     });
// }


